package com.kyrie.lib_sys.utils.learn.TimeDemo;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class TimeDemo03 {
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static List<Map<String, String>> timeList = new ArrayList<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入要添加的时间记录数量：");
        int count = Integer.parseInt(scanner.nextLine());

        for (int i = 0; i < count; i++) {
            System.out.println("第" + (i + 1) + "条记录：");

            System.out.print("请输入 prepareTime（格式 yyyy-MM-dd HH:mm:ss）：");
            String prepareTime = scanner.nextLine();

            System.out.print("请输入 travelTime（格式 yyyy-MM-dd HH:mm:ss）：");
            String travelTime = scanner.nextLine();

            System.out.print("请输入 startTime（格式 yyyy-MM-dd HH:mm:ss）：");
            String startTime = scanner.nextLine();

            System.out.print("请输入 completeTime（格式 yyyy-MM-dd HH:mm:ss）：");
            String completeTime = scanner.nextLine();

            timeList.add(Map.of(
                    "prepareTime", prepareTime,
                    "travelTime", travelTime,
                    "startTime", startTime,
                    "completeTime", completeTime
            ));
        }

        //进行工时校验
        System.out.println(timeCheck(timeList));
    }

    /*  正确的时间
        2025-09-01 11:11:11
        2025-09-01 12:12:12
        2025-09-01 13:13:13
        2025-09-01 14:14:14


    */

    /*
        存在时间重叠
        2025-09-01 11:11:11
        2025-09-01 13:13:13
        2025-09-01 12:12:12
        2025-09-01 14:14:14

        存在时间重叠
        2025-09-01 11:11:11
        2025-09-01 10:10:10
     */

    /**
     * 工时时间校验
     * @param timeList
     * @return
     */
    public static boolean timeCheck(List<Map<String, String>> timeList) {
        if (timeList == null || timeList.isEmpty()) {
            System.out.println("timeList没有数据");
            return false;
        }

        for (Map<String, String> timeMap : timeList) {

            List<LocalDateTime> times = new ArrayList<>();

            // 只将存在的、非空的时间点加入times集合列表中
            if (timeMap.containsKey("prepareTime") && !timeMap.get("prepareTime").trim().isEmpty()) {
                times.add(LocalDateTime.parse(timeMap.get("prepareTime").trim(), formatter));
            }
            if (timeMap.containsKey("travelTime") && !timeMap.get("travelTime").trim().isEmpty()) {
                times.add(LocalDateTime.parse(timeMap.get("travelTime").trim(), formatter));
            }
            if (timeMap.containsKey("startTime") && !timeMap.get("startTime").trim().isEmpty()) {
                times.add(LocalDateTime.parse(timeMap.get("startTime").trim(), formatter));
            }
            if (timeMap.containsKey("completeTime") && !timeMap.get("completeTime").trim().isEmpty()) {
                times.add(LocalDateTime.parse(timeMap.get("completeTime").trim(), formatter));
            }

            //遍历times时间集合,获取每一个时间值,进行校验
            for (int i = 0; i < times.size() - 1; i++) {
                for (int j = i + 1; j < times.size(); j++) {

                    // 校验是否存在时间重叠，要是说后一个时间小于前一个时间或者后一个时间与前一个时间相同，则判定为重叠
                    if (!times.get(j).isAfter(times.get(i)) || times.get(i).equals(times.get(j))) {
                        System.out.println("时间: "+ times.get(j) + " 存在重叠");
                        return false;
                    }
                }
            }
        }



        System.out.println("时间段没有重叠");
        return true;
    }


}
